/*
 * Copyright (C) 2010 M. Hautle.
 * 
 * This file is part of SimpleEL.
 * 
 * SimpleEL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * SimpleEL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with SimpleEL. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.simpleel.util;

import java.beans.*;
import java.lang.reflect.*;
import java.util.*;
import ch.simpleel.data.*;
import ch.simpleel.internal.reflection.*;

/**
 * Optimized reflection implementation for {@link MiscUtils#copyValues(Object, Object)} and {@link MiscUtils#copyValuesTo(Object, Object, String...)}.
 * 
 * @author M. Hautle
 */
public class MiscReflectionTest extends Common {
    private final PropertyDescriptor[] props;

    private final Map<String, PropertyDescriptor> dstProps = ReflectionHelper.getProperties(AOtherDom.class);

    private final Object[] args = new Object[1];

    /**
     * 
     */
    public MiscReflectionTest() throws Exception {
        props = Introspector.getBeanInfo(TheDom.class).getPropertyDescriptors();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void executeCopy(TheDom dom, TheDom dom2) throws Exception {
        for (PropertyDescriptor p : props) {
            final Method read = p.getReadMethod();
            final Method write = p.getWriteMethod();
            if (read != null && write != null) {
                final Object val = read.invoke(dom, (Object[]) null);
                args[0] = val;
                write.invoke(dom2, args);
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void executeCopy(TheDom dom, AOtherDom dom2) throws Exception {
        for (PropertyDescriptor p : props) {
            final String name = p.getName();
            final PropertyDescriptor dProp = dstProps.get(name);
            if (dProp == null)
                continue;
            final Method read = p.getReadMethod();
            final Method write = dProp.getWriteMethod();
            if (read != null && write != null) {
                final Object val = read.invoke(dom, (Object[]) null);
                args[0] = val;
                write.invoke(dom2, args);
            }
        }
    }

    /**
     * VM Args:<br>
     * 
     * <pre>
     * -server / -client
     * -Xms512m -Xmx512m
     * -XX:+PrintCompilation 
     * -verbose:gc
     * </pre>
     * 
     * @param args
     */
    public static void main(String[] args) throws Exception {
        new MiscReflectionTest().executeBenchmark();
    }
}
